package com.cy.util;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.bean.AdminList;
import com.cy.bean.UserList;
import com.cy.service.LoginService;
import com.cy.service.LoginServiceImpl;

/**
 * Servlet Filter implementation class LoginFilter
 */
@WebFilter({ "/adduser", "/userlist", "/addgame", "/gamelist", "/change", "/main", "/buygame", "/aboutme" })
public class LoginFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		// 判断登录用户类型
		String judge = (String) request.getSession().getAttribute("judge");
		if (judge != null && judge.equals("0")) {
			// 普通用户
			UserFilter(request, response, chain);
		} else if (judge != null && judge.equals("1")) {
			// 管理员
			AdminFilter(request, response, chain);

		} else if (judge == null) {
			Cookie cookie = CookieUtil.getCookieByName(request, "cookie_name_pass_judge");
			if (cookie != null) {
				String userAndpass = URLDecoder.decode(cookie.getValue(), "utf-8");
				// 分割用户名与密码通过#
				String[] up = userAndpass.split("#");
				judge = up[2];
				if (judge != null && judge.equals("0")) {
					// 普通用户
					UserFilter(request, response, chain);
				} else if (judge != null && judge.equals("1")) {
					// 管理员
					AdminFilter(request, response, chain);
				} else if (judge == null) {
					// 未登录就跳转到登录页面
					request.setAttribute("error", "保存的用户名/密码过期");
					request.getRequestDispatcher("/index.jsp").forward(request, response);
				}
			} else {
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			}
		}

	}

	private void AdminLoginFilter(AdminList admin, HttpServletRequest request, HttpServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String servleturl = request.getServletPath();
		if (servleturl.equals("/main") || servleturl.equals("/adduser") || servleturl.equals("/userlist")
				|| servleturl.equals("/addgame") || servleturl.equals("/gamelist") || servleturl.equals("/change")) {
			request.getSession().setAttribute("adminuser", admin);
			chain.doFilter(request, response);
		} else {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// 两秒后跳转
			request.setAttribute("error", "无权限访问");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}

	}

	private void UserLoginFilter(UserList user, HttpServletRequest request, HttpServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String servleturl = request.getServletPath();
		if (servleturl.equals("/buygame") || servleturl.equals("/aboutme") || servleturl.equals("/change")) {
			request.getSession().setAttribute("gengraluser", user);
			chain.doFilter(request, response);
		} else {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// 两秒后跳转
			request.setAttribute("error", "无权限访问");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}

	private void UserFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		UserList user = (UserList) request.getSession().getAttribute("gengraluser");
		if (user != null) {
			// 普通用户登录过滤
			UserLoginFilter(user, request, response, chain);
		} else {
			Cookie cookie = CookieUtil.getCookieByName(request, "cookie_name_pass_judge");
			if (cookie != null) {
				String userAndpass = URLDecoder.decode(cookie.getValue(), "utf-8");
				LoginService loginService = new LoginServiceImpl();
				// 分割用户名与密码通过#
				String[] up = userAndpass.split("#");
				UserList user2 = loginService.findUser(up[0], up[1]);
				// 判断当前用户名与密码是否有效
				if (user2 != null) {
					request.getSession().setAttribute("gengraluser", user2);
					chain.doFilter(request, response);
				} else {
					// 未登录就跳转到登录页面
					request.setAttribute("error", "保存的用户名/密码过期");
					request.getRequestDispatcher("/index.jsp").forward(request, response);
				}
			} else {
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			}
		}
	}

	private void AdminFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		AdminList admin = (AdminList) request.getSession().getAttribute("adminuser");
		if (admin != null) {
			// 管理员登录
			AdminLoginFilter(admin, request, response, chain);
		} else {
			Cookie cookie = CookieUtil.getCookieByName(request, "cookie_name_pass_judge");
			if (cookie != null) {
				String userAndpass = URLDecoder.decode(cookie.getValue(), "utf-8");
				LoginService loginService = new LoginServiceImpl();
				// 分割用户名与密码通过#
				String[] up = userAndpass.split("#");
				AdminList admin2 = loginService.findAdmin(up[0], up[1]);
				// 判断当前用户名与密码是否有效
				if (admin2 != null) {
					request.getSession().setAttribute("adminuser", admin2);
					chain.doFilter(request, response);
				} else {
					// 未登录就跳转到登录页面
					request.setAttribute("error", "保存的用户名/密码过期");
					request.getRequestDispatcher("/index.jsp").forward(request, response);
				}
			} else {
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			}
		}
	}

	/**
	 * 
	 */
	public void init(FilterConfig arg0) throws ServletException {
	}
}
